package com.zzyl.nursing.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.dto.AlertDataDto;
import com.zzyl.nursing.dto.HandleAlertDataDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

/**
 * 报警数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-12
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;



    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     * 
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData)
    {
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData) ? 1 : 0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 条件分页查询报警数据列表
     *
     * @param alertDataDto 报警数据
     * @return 报警数据
     */

    @Override
    public List<AlertData> selectAlertDataByPage(AlertDataDto alertDataDto) {
        LambdaQueryWrapper<AlertData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(alertDataDto.getDeviceName() != null && !alertDataDto.getDeviceName().isEmpty(),AlertData::getDeviceName, alertDataDto.getDeviceName())
                .ge(alertDataDto.getStartTime() != null,AlertData::getCreateTime,alertDataDto.getStartTime())
                .lt(alertDataDto.getEndTime() != null,AlertData::getCreateTime,alertDataDto.getEndTime())
                .eq(alertDataDto.getStatus() != null,AlertData::getStatus, alertDataDto.getStatus());
        List<AlertData> list = this.list(wrapper);
        return list;
    }

    /**
     * 处理报警数据
     *
     * @param handleAlertDataDto 报警数据
     * @return 结果
     */

    @Override
    public void handleAlertData(HandleAlertDataDto handleAlertDataDto) {
        AlertData alertData = alertDataMapper.selectById(handleAlertDataDto.getId());
        String userName =  alertDataMapper.selectUserNameById(alertData.getUserId());
        LambdaUpdateWrapper<AlertData> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(AlertData::getProcessorId, alertData.getUserId())
                .set(AlertData::getProcessingTime, handleAlertDataDto.getProcessingTime())
                .set(AlertData::getProcessingResult, handleAlertDataDto.getProcessingResult())
                .set(AlertData::getStatus, 1)
                .set(AlertData::getProcessorName,userName )
                .eq(AlertData::getId, handleAlertDataDto.getId());
        this.update(wrapper);
    }


}
